package org.one.stone.soup.grfxML.plugin.geometry;

import org.one.stone.soup.grfxML.DataPoint;
import org.one.stone.soup.grfxML.DataPoints;
import org.one.stone.soup.grfxML.DataSize;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.GrfxMLProcessor;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.math.IntMath;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.IntField;
import org.one.stone.soup.mjdb.data.field.LockException;

public class ThickLine extends SimplePlugin {

	public static final int ARG_START = 0;
	public static final int ARG_END = 1;
	public static final int ARG_THICKNESS = 2;
	public static final int ARG_POINTS = 3;

	DataPoint start = new DataPoint();
	DataPoint end = new DataPoint();
	DataSize thickness = new DataSize();
	DataPoints points = new DataPoints();
/**
 * StateInverter constructor comment.
 */
public ThickLine(GrfxMLEngine engine){
	super(engine);
}
/**
 * initialize method comment.
 */
public void initialize()
{
}
/**
 * process method comment.
 */
public void process()
{
 try{
	if( start.hasChanged() || end.hasChanged() )
	{
		int x = end.getValueX()-start.getValueX();
		int y = end.getValueY()-start.getValueY();
		IntField angle = new IntField( IntMath.angle(x,y) );
		int length = IntMath.distance(x,y);

		points.setEmpty();

		DataPoint point = new DataPoint();
		point.setValueX( 0,this );
		point.setValueY( (-thickness.getValue())/2,this );
		GrfxMLProcessor.rotate( point,angle,point );
		points.add( point,this );

		point = new DataPoint();
		point.setValueX( 0,this );
		point.setValueY( thickness.getValue()/2,this );
		GrfxMLProcessor.rotate( point,angle,point );
		points.add( point,this );

		point = new DataPoint();
		point.setValueX( length,this );
		point.setValueY( thickness.getValue()/2,this );
		GrfxMLProcessor.rotate( point,angle,point );
		points.add( point,this );

		point = new DataPoint();
		point.setValueX( length,this );
		point.setValueY( -(thickness.getValue())/2,this );
		GrfxMLProcessor.rotate( point,angle,point );
		points.add( point,this );
	}
 }catch(LockException le){}
}
/**
 * register method comment.
 */
public void register(DataLinker store)
{

	start = grfxMLCaster.cast(start,getArg(start,ARG_START,store));
	end = grfxMLCaster.cast(end,getArg(end,ARG_END,store));
	thickness = grfxMLCaster.cast(thickness,getArg(thickness,ARG_THICKNESS,store));
	points = grfxMLCaster.cast(points,getArg(points,ARG_POINTS,store));

}
/**
 * replace method comment.
 */
public void replace(Field oldObj, Field newObj)
{

	start = grfxMLCaster.replace(start,oldObj,newObj);
	end = grfxMLCaster.replace(end,oldObj,newObj);
	thickness = grfxMLCaster.replace(thickness,oldObj,newObj);
	points = grfxMLCaster.replace(points,oldObj,newObj);

}
/**
 * stop method comment.
 */
public void stop() {}
}
